#include "stdafx.h"
#include <MyControls/PropertyDlg.h>
#include <MyControls/Resource.h>

namespace My {

// PropertyDlg dialog

PropertyDlg::PropertyDlg(String caption, CSize size, CWnd * pParent)
{
	m_Caption = caption;
	m_Size = size;
	m_pObject = 0;

	m_pParentWnd = pParent;
	m_lpszTemplateName = MAKEINTRESOURCE(IDD_PROPERTIES);
	m_nIDHelp = 0;

	m_Font.CreateFont(-11, 0, 0, 0, 400, FALSE, FALSE, 0, DEFAULT_CHARSET, 0, 0, 0, 0, "Tahoma"); 
	m_Editor.SetFont(&m_Font);
	m_Editor.SetInfoHeight(0);

	Add(&m_Editor);
}

PropertyDlg::~PropertyDlg()
{
}

bool PropertyDlg::Open(EditableObject * pObject)
{
	if (pObject == 0)
		return false;
	m_pObject = pObject;
	m_Editor.Open(pObject);

	return DoModal() == IDOK;
}

void PropertyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDOK, m_Ok);
	DDX_Control(pDX, IDCANCEL, m_Cancel);

	if (pDX->m_bSaveAndValidate) {
		/// 
	}
}

BEGIN_MESSAGE_MAP(PropertyDlg, My::CustomWindow<CDialog>)
	ON_WM_SIZE()
	ON_WM_SIZING()
	ON_WM_CREATE()
	ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
END_MESSAGE_MAP()


// PropertyDlg message handlers

void PropertyDlg::OnSize(UINT nType, int cx, int cy)
{
	My::CustomWindow<CDialog>::OnSize(nType, cx, cy);

	if (m_Ok.m_hWnd == 0)
		return;

	CRect r = ClientRect();

	CRect rCancel;
	m_Cancel.GetWindowRect(&rCancel);
	CRect rOk;
	m_Ok.GetWindowRect(&rOk);

	HDWP pPos = BeginDeferWindowPos(2);
	DeferWindowPos(pPos, m_Cancel.m_hWnd, 0, r.right - 4 - rCancel.Width(), r.bottom - 4 - rCancel.Height(), 0, 0, SWP_NOSIZE);
	DeferWindowPos(pPos, m_Ok.m_hWnd, 0, r.right - 4 - rCancel.Width() - 4 - rOk.Width(), r.bottom - 4 - rOk.Height(), 0, 0, SWP_NOSIZE);
	EndDeferWindowPos(pPos);

}

void PropertyDlg::OnSizing(UINT fwSide, LPRECT pRect)
{
	const int W = 160, H = 40;
	if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT || fwSide == WMSZ_BOTTOMLEFT) {
		// adjust left
		if (pRect->left > pRect->right - W)
			pRect->left = pRect->right - W;
	}
	if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT || fwSide == WMSZ_BOTTOMRIGHT) {
		// adjust right
		if (pRect->right < pRect->left + W)
			pRect->right = pRect->left + W;
	}
	if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT || fwSide == WMSZ_TOPRIGHT) {
		// adjust top
		if (pRect->top > pRect->bottom - H)
			pRect->top = pRect->bottom - H;
	}
	if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT || fwSide == WMSZ_BOTTOMRIGHT) {
		// adjust bottom
		if (pRect->bottom < pRect->top + H)
			pRect->bottom = pRect->top + H;
	}
}

bool PropertyDlg::CanGetFocus() const
{
	return true;
}

void PropertyDlg::LayoutControls()
{
	CRect r = ClientRect();
	r.DeflateRect(4, 4);
	r.bottom -= 20;
	m_Editor.SetBounds(r);
}

void PropertyDlg::HandlePaint(CDC * pDC, const CRect & rClip)
{
	CBrush brush; brush.CreateSysColorBrush(COLOR_BTNFACE);
	pDC->FillRect(&rClip, &brush);
}

LRESULT PropertyDlg::OnKickIdle(WPARAM, LPARAM)
{
	AfxCallWndProc(this, this->m_hWnd,
		WM_IDLEUPDATECMDUI, (WPARAM)TRUE, 0);
	SendMessageToDescendants(WM_IDLEUPDATECMDUI,
		(WPARAM)TRUE, 0, TRUE, TRUE);
	return 0;
}

int PropertyDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (Base::OnCreate(lpCreateStruct) == -1)
		return -1;
	ModifyStyleEx(0, WS_EX_DLGMODALFRAME|WS_EX_WINDOWEDGE);
	return 0;
}

BOOL PropertyDlg::OnInitDialog()
{
	if (!Base::OnInitDialog())
		return FALSE;
	if (m_Size.cx > 0 && m_Size.cy > 0)
		SetWindowPos(0, 0, 0, m_Size.cx, m_Size.cy, SWP_NOMOVE);
	SetWindowText(m_Caption.c_str());
	return TRUE;
}

} // My
